864C - Bus - CodeForces Solution


greedy implementation math *1500

Please click on ads to support us..

Python Code:

l=[int(x) for x in input().split()]
a=[]
b=l[1]
f=0
c=0
 
a.append(0)
 
for i in range(l[3]):
    if(i%2==0):
        a.append(i*l[0]+l[2])
    else:
        a.append((i+1)*l[0]-l[2])
a.append(l[0]*l[3])
 
for j in range(1,l[3]+2):
    
    if(b>=(a[j]-a[j-1])):
        b=b-a[j]
        b=b+a[j-1]
        
    else:
        
        b=l[1]
        if(b>=a[j]-a[j-1]):
            f=f+1
            b=b-(a[j]-a[j-1])
        else:
            c=-1 
            break
if(c==-1):
    print(c)
else:
    print(f)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a,b,f,k;
int main(){
	cin>>a>>b>>f>>k;
	int t=0,b1=b,i=0,ans=0;
	while(k--){
		if(i==0){
			t=f;
			b1=b1-f;
			if(b1<0){
				cout<<-1;
				return 0;
			}
			if(b1<2*(a-f)&&k!=0){
				b1=b;
				ans++;
			}
			else if(k==0&&b1<a-f){
				b1=b;
				ans++;
			}
			t=a;
			b1=b1-(a-f);
			
			if(b1<0){
				cout<<-1;
				return 0;
			}
			i=1;
		}
		else if(i==1){
			t=f;
			b1=b1-(a-f);
			if(b1<0){
				cout<<-1;
				return 0;
			}
			if(b1<2*f&&k!=0){
				b1=b;
				ans++;
			}
			else if(k==0&&b1<f){
				b1=b;
				ans++;
			}
			t=0;
			b1=b1-f;
			if(b1<0){
				cout<<-1;
				return 0;
			}
			i=0;
		}
//		cout<<b1<<" "<<ans<<endl;
	}
	cout<<ans;
	return 0;
}
 				   	  		 	 	 	  			   	 	


Comments

Submit
0 Comments
More Questions

1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll
1430B - Barrels
279B - Books
1374B - Multiply by 2 divide by 6
1093B - Letters Rearranging
1213C - Book Reading
1468C - Berpizza
1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel